﻿/*****************************************************************
 *@Copyright all rights reserved © 2014 Yuga
 *@author YUga studio
 *Contact: zengkang007@gmail.com
 *****************************************************************/
package com.oplink.webbase.modules.invoicing.service;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.oplink.webbase.common.persistence.Page;
import com.oplink.webbase.modules.invoicing.dao.StockDao;
import com.oplink.webbase.modules.invoicing.entity.Stock;
import com.oplink.webbase.modules.invoicing.entity.SubStock;

/**
 * 告警Service
 * 
 * @author YUga studio
 * @version 2015-01-12
 */
@Component
@Transactional
public class StockService {

	@SuppressWarnings("unused")
	private static Logger logger = LoggerFactory.getLogger(StockService.class);

	@Autowired
	private StockDao stockDao;

	public Stock get(Long id) {
		return stockDao.findOne(id);
	}

	/**
	 * 是否存在指定的货物。
	 * 
	 * @return
	 */
	public boolean isExistGoods(String gid) {
		return (stockDao.isExistGoods(gid) > 0);
	}

	/**
	 * 查询子单
	 * 
	 * @param gid 货物ID
	 * @return
	 */
	public SubStock getSubStock(String gid) {
		return stockDao.getSubStock(gid);
	}

	public Page<Stock> find(Page<Stock> page, Stock stock) {
		DetachedCriteria dc = stockDao.createDetachedCriteria();
		// 添加查询条件
		if ((null != stock.getStockFormId()) && StringUtils.isNotEmpty(stock.getStockFormId().toString())) {
			dc.add(Restrictions.like("stockFormId", "%" + stock.getStockFormId() + "%"));
		}
		if (StringUtils.isNotEmpty(stock.getStockTime())) {
			dc.add(Restrictions.like("stockTime", "%" + stock.getStockTime() + "%"));
		}
		if ((null != stock.getOperatman()) && StringUtils.isNotEmpty(stock.getOperatman().toString())) {
			dc.add(Restrictions.like("operatman", "%" + stock.getOperatman() + "%"));
		}
		dc.add(Restrictions.eq(Stock.DEL_FLAG, Stock.DEL_FLAG_NORMAL));
		dc.addOrder(Order.desc("id"));
		return stockDao.find(page, dc);
	}

	/**
	 * @function 创建查询
	 **/
	public Page<Stock> getUserStock(Page<Stock> page, Stock stock) {
		DetachedCriteria dc = stockDao.createDetachedCriteria();
		// 添加查询条件
		if ((null != stock.getStockFormId()) && StringUtils.isNotEmpty(stock.getStockFormId().toString())) {
			dc.add(Restrictions.like("stockFormId", "%" + stock.getStockFormId() + "%"));
		}
		if (StringUtils.isNotEmpty(stock.getStockTime())) {
			dc.add(Restrictions.like("stockTime", "%" + stock.getStockTime() + "%"));
		}
		if ((null != stock.getOperatman()) && StringUtils.isNotEmpty(stock.getOperatman().toString())) {
			dc.add(Restrictions.like("operatman", "%" + stock.getOperatman() + "%"));
		}
		dc.add(Restrictions.eq(Stock.DEL_FLAG, Stock.DEL_FLAG_NORMAL));
		dc.addOrder(Order.desc("id"));
		return stockDao.find(page, dc);
	}

	public Stock getStock(Long id) {
		return stockDao.findOne(id);
	}

	public void saveStock(Stock entity) {
		stockDao.save(entity);
	}

	public void deleteStock(Long id) {
		stockDao.delete(id);
	}

	@Autowired
	public void setStockDao(StockDao stockDao) {
		this.stockDao = stockDao;
	}
}
